<template>
  <el-dialog
    title="注册"
    :visible.sync="dialogVisible"
    width="25%"
    :before-close="handleClose"
    :append-to-body="true"
  >
    <el-row>
      <el-form
        label-width="80px"
        ref="createForm"
        :rules="rules"
        :model="formData"
      >
        <el-form-item label="租户代码" prop="tenancyName">
          <el-input v-model="formData.tenancyName" />
        </el-form-item>
        <el-form-item label="租户名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="电子邮箱" prop="adminEmailAddress">
          <el-input v-model="formData.adminEmailAddress" />
        </el-form-item>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose()">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="comit()"
        >确 定</el-button
      >
    </span>
    <checkCode ref="checkCode"></checkCode>
  </el-dialog>
</template>
<script>
// import { CreateOrUpdate } from '@api/systemManage/menuManage'
import { Register } from '@api/systemManage/tenantManage'
import checkCode from '@/pages/login/checkCode'
export default {
  components: { checkCode },
  data () {
    return {
      title: '',
      dialogVisible: false,
      loading: false,
      formData: {
        tenancyName: '',
        name: '',
        adminEmailAddress: '',
        isActive: false
      },
      rules: {
        tenancyName: [
          { required: true, message: '请输入租户代码', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入租户名称', trigger: 'blur' }
        ],
        adminEmailAddress: [
          { required: true, message: '请输入电子邮箱', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    comit () {
      this.$refs.createForm.validate((valid) => {
        if (valid) {
          this.$message('正在注册，请勿离开页面')
          Register(this.formData).then(res => {
            if (res.success) {
              var obj = {
                tenantCode: this.formData.tenancyName,
                mobileOrEmail: this.formData.adminEmailAddress,
                type: 'Tenant'
              }
              this.$refs.checkCode.show(obj)
              this.handleClose()
              this.$message({
                message: '注册成功,激活验证码已下发至邮箱，请查收！',
                type: 'success'
              })
            } else {
              this.$message.error('请求错误~')
            }
          })
        }
      })
    },
    show () {
      this.dialogVisible = true
    },
    handleClose () {
      this.formData = {
        tenancyName: '',
        name: '',
        adminEmailAddress: ''
      }
      this.dialogVisible = false
    }
  }
}
</script>
